﻿

using System;
using System.Data;
using Model;
namespace Dal
{
	public class DbRoleDal
	{
		public DbRoleDal()
		{
		}
 		#region Insert
 		public static int Insert(DbRole dbRole)
		{	
			string sql = @"Insert Into DbRole (
					RRoleName,
					RRoleCode,
					RNote
				) values('{0}','{1}','{2}')";
			sql = string.Format(sql,
					SqlHelper.String(dbRole.RRoleName),
					SqlHelper.String(dbRole.RRoleCode),
					SqlHelper.String(dbRole.RNote)
				);
			return SqlHelper.ExecSql(sql);
		}
		#endregion
		
		#region Update
 		public static int Update(DbRole dbRole)
		{	
			string sql = @"Update DbRole set 

					RRoleName= '{0}',
					RRoleCode= '{1}',
					RNote= '{2}'
				where

					RId= {3}";
			sql = string.Format(sql,
					SqlHelper.String(dbRole.RRoleName),
					SqlHelper.String(dbRole.RRoleCode),
					SqlHelper.String(dbRole.RNote),
					dbRole.RId);
			return SqlHelper.ExecSql(sql);
		}
		#endregion
 
 

 		#region Delete
 		public static int Delete(Int32 rId)
		{
		
			string sql = @"Delete from DbRole where 
					RId= {0}";
			sql = string.Format(sql,
					rId
);
			return SqlHelper.ExecSql(sql);
		} 
		#endregion
 
 		#region Select
 		
 		public static DataSet Select()
		{
			string sql = "select a.* from DbRole a "; 
			return SqlHelper.Select(sql);
		}
				
		public static DbRole Select(Int32 rId)
		{
			DbRole dbRole = null;
			string sql = @"select a.* from DbRole a  where
					a.RId= {0}
";
			sql = string.Format(sql,
					rId
);
			DataSet ds = SqlHelper.Select(sql);
			if(SqlHelper.HasRow(ds))
				dbRole = new DbRole(ds.Tables[0].Rows[0]);					
			return dbRole;
		}
		
		public static DataSet Select(string where)
		{ 
			if (where.Trim().ToLower().StartsWith("where"))
				where = where.Substring(5);
			if (!where.Trim().ToLower().StartsWith("and") && where.Trim().Length > 0)
				where = " and " + where;
			string sql = "select a.* from DbRole a  where 1=1 " + where ;
			return SqlHelper.Select(sql);
		}
		
		public static DataSet SelectForList(string where,int pageIndex,int pageSize,bool bCount,out int count )
		{
			count = 0;
						
			if (where.Trim().ToLower().StartsWith("where"))
				where = where.Substring(5);
			if (!where.Trim().ToLower().StartsWith("and") && where.Trim().Length > 0)
				where = " and " + where;
			string sql = "select a.* from dbRole a where 1=1 " + where;
                
			return SqlHelper.Select(sql,pageSize,pageIndex,bCount,ref count);
		}		
		public static DataSet Select(string where,int pageIndex,int pageSize,bool bCount,out int count )
		{
			count = 0;			
			if (where.Trim().ToLower().StartsWith("where"))
				where = where.Substring(5);
			if (!where.Trim().ToLower().StartsWith("and") && where.Trim().Length > 0)
				where = " and " + where;			
			string sql = "select a.* from DbRole a  where 1=1 " + where;
			return SqlHelper.Select(sql,pageSize,pageIndex,bCount,ref count);
		}
		#endregion
	}
}
